package cn.qu.phizline.connect;

import android.os.Handler;
import android.os.HandlerThread;

import org.webrtc.IceCandidate;
import org.webrtc.SessionDescription;

import cn.qu.phizline.AndroidPayloadConventer;
import cn.qu.phizline.PhizlineApplication;
import cn.qu.phizline.connect.channel.WebSocketChannelClient;
import cn.qu.phizline.event.login.LoginEvent;

import static cn.qu.phizline.activity.IntentExtraKeys.USERID;
import static cn.qu.phizline.activity.IntentExtraKeys.USERPWD;

/**
 * 基于websocket的服务器<->客户端连接
 */
public class WebSocketRTCClient implements AppRTCClient, WebSocketChannelEvents {

    private static final String TAG = WebSocketRTCClient.class.getSimpleName();
    private final Handler handler;
    /**
     * 连接状态
     */
    private ConnectionState state;
    /**
     * 信令回调接口
     */
    private SignalingEvents events;

    /**
     * 房间参数
     */
    private RoomConnectionParameters connectionParameters;

    /**
     * WS客户端
     */
    private WebSocketChannelClient wsClient;
    private String url;

    public WebSocketRTCClient(String url /*SignalingEvents events*/) {
        this.url = url;
        state = ConnectionState.NEW;
        final HandlerThread handlerThread = new HandlerThread(TAG);
        handlerThread.start();
        handler = new Handler(handlerThread.getLooper());
        wsClient = new WebSocketChannelClient(handler, this);
    }

    public void connect() {
        wsClient.connect(url);
    }

    @Override
    public void doLogin(String userid, String passwd) {
        LoginEvent event = new LoginEvent();
        LoginEvent.LoginBody body = new LoginEvent.LoginBody();

        body.setUser(userid);
        body.setPasswd(passwd);

        event.setBody(body);
        wsClient.send(new AndroidPayloadConventer().toBytes(event));
    }

    @Override
    public void doLogin(String token) {
        LoginEvent event = new LoginEvent();
        LoginEvent.LoginBody body = new LoginEvent.LoginBody();

        body.setToken(token);

        event.setBody(body);
        wsClient.send(new AndroidPayloadConventer().toBytes(event));
    }

    @Override
    public void onInit(boolean status, String publishKey) {

        //需要登录
        if (status == false) {
            PhizlineApplication.setToken("");
            String userid = PhizlineApplication.getAttr(USERID, "1");
            String passwd = PhizlineApplication.getAttr(USERPWD, "2");
            doLogin(userid, passwd);
        } else {
            //需要刷新
            String token = PhizlineApplication.getToken();
            doLogin(token);
        }

    }

    @Override
    public void onLogin(boolean status, String token) {
        if (status) {
            state = ConnectionState.LOGINED;
            PhizlineApplication.setToken(token);
        }
    }

    @Override
    public void onWebSocketClose() {

    }

    @Override
    public void onWebSocketError(String description) {

    }

    private enum ConnectionState {NEW, CONNECTED, LOGINED, CLOSED, ERROR}

    @Override
    public void connectToRoom(RoomConnectionParameters connectionParameters) {
        this.connectionParameters = connectionParameters;
        handler.post(new Runnable() {
            @Override
            public void run() {
                connectToRoomInternal();
            }
        });
    }

    // Connects to room - function runs on a local looper thread.
    private void connectToRoomInternal() {
        String connectionUrl = connectionParameters.roomUrl;
        state = ConnectionState.NEW;

    }

    @Override
    public void sendOfferSdp(SessionDescription sdp) {

    }

    @Override
    public void sendAnswerSdp(SessionDescription sdp) {

    }

    @Override
    public void sendLocalIceCandidate(IceCandidate candidate) {

    }

    @Override
    public void sendLocalIceCandidateRemovals(IceCandidate[] candidates) {

    }

    @Override
    public void disconnectFromRoom() {

    }

}
